package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.AddGoodsIdentify;
import com.woniuxy.controller.form.FindGoodsIf;
import com.woniuxy.dao.AccountDao;
import com.woniuxy.dao.PawnGoodsDao;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ParameterException;
import com.woniuxy.model.Account;
import com.woniuxy.model.PawnGoods;
import com.woniuxy.model.PawnGoodsIdentifyLog;
import com.woniuxy.dao.PawnGoodsIdentifyLogDao;
import com.woniuxy.service.PawnGoodsIdentifyLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
@Transactional
public class PawnGoodsIdentifyLogServiceImpl extends ServiceImpl<PawnGoodsIdentifyLogDao, PawnGoodsIdentifyLog> implements PawnGoodsIdentifyLogService {
    @Resource
    private  PawnGoodsIdentifyLogDao goodsIdentifyLogDao;
    @Resource
    private PawnGoodsDao goodsDao;
    @Resource
    private AccountDao ad;
    @Override

    public Result addgoodsIdentifyLong(AddGoodsIdentify addGoodsIdentify, BindingResult bindingResult)throws DdException {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            String s="";
            for (ObjectError allError : allErrors) {
                s = allError.getDefaultMessage();
            }
            throw new ParameterException(500003, s);
        }

        QueryWrapper<PawnGoodsIdentifyLog> qw = new QueryWrapper<>();
         qw.eq("stat", 1);
         qw.eq("goods_id",addGoodsIdentify.getGoodsId());
        PawnGoodsIdentifyLog goodsIdentifyLog = baseMapper.selectOne(qw);
        if(ObjectUtils.isEmpty(goodsIdentifyLog)){
            addGoodsIdentify.setCreateTime(new Date());
            PawnGoodsIdentifyLog pawnGoodsIdentifyLog = NewCopyBean.copyOne(addGoodsIdentify, PawnGoodsIdentifyLog.class);
            baseMapper.insert(pawnGoodsIdentifyLog);
        }else{
            addGoodsIdentify.setCreateTime(new Date());
            PawnGoodsIdentifyLog pawnGoodsIdentifyLog = NewCopyBean.copyOne(addGoodsIdentify, PawnGoodsIdentifyLog.class);
            baseMapper.insert(pawnGoodsIdentifyLog);
            goodsIdentifyLog.setStat("0");
            baseMapper.update(goodsIdentifyLog,new QueryWrapper<PawnGoodsIdentifyLog>()
                    .eq("identify_id",goodsIdentifyLog.getIdentifyId()));
        }
        PawnGoods pawnGoods = goodsDao.selectById(addGoodsIdentify.getGoodsId());
        Account account = ad.selectOne(new QueryWrapper<Account>().eq("account_username", addGoodsIdentify.getCreateBy()));
        pawnGoods.setSurveyor(account.getAccountId());
        pawnGoods.setGoodsStat("2");
        goodsDao.updateById(pawnGoods);
        return Result.success(200,"新增成功",null);
    }

    @Override
    public Result selsectgoodsdentifyLog(FindGoodsIf findGoodsIf) {
    Page<PawnGoodsIdentifyLog> pawnGoodsIdentifyLogPage=baseMapper.selectPage(findGoodsIf,new QueryWrapper<PawnGoodsIdentifyLog>().eq("goods_id",findGoodsIf.getGoodsId()));
        return Result.success(200,"查询成功",pawnGoodsIdentifyLogPage);
    }

    @Override
    public Result selsectOldgoodsdentifyLog(Integer goods) {
        List<PawnGoodsIdentifyLog> pawnGoodsIdentifyLogs = baseMapper.selectList(new QueryWrapper<PawnGoodsIdentifyLog>().eq("good_id", goods)
                .eq("stat", 0));
        return Result.success(200,"查询成功",pawnGoodsIdentifyLogs);
    }
}
